﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using StateGrid;
using StateGrid.User;
using StateGrid.OperationLog;
using StateGrid.GlobalVariables;

public partial class Pages_DailyRecord_DailyRecordIndex : System.Web.UI.Page
{
    /// <summary>
    /// 用户操作对象
    /// </summary>
    private UserManageDAO userDAO = new UserManageDAO();
    /// <summary>
    /// 日志操作对象
    /// </summary>
    private OperationLog_DAO logDAO = new OperationLog_DAO();

    protected void Page_Load(object sender, EventArgs e)
    {
        
        if (!UserManageDAO.IsLogin((User)Session["login"]))
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        if (!this.IsPostBack)
        {
            this.InitData();
        }
    }

    
    /// <summary>
    /// 初始化数据
    /// </summary>
    private void InitData()
    {
        this.BindWKData();
        this.BindStaffData();
        this.SetVisible(false);
    }

    /// <summary>
    /// 绑定文库
    /// </summary>
    private void BindWKData()
    {
        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }
        //清空列表
        this.wkDropDownList.Items.Clear();

        //重新绑定
        IList<FunctionModular> functionList = user.grantedModulars;
        if (null == functionList||functionList.Count==0)
        {
            return;
        }
        foreach (FunctionModular temp in functionList)
        {
            ListItem item = new ListItem(temp.name, temp.id);
            this.wkDropDownList.Items.Add(item);
        }
    }

    /// <summary>
    /// 绑定工作人员
    /// </summary>
    private void BindStaffData()
    {
        //清空人员列表
        this.staffDropDownList.Items.Clear();
        User user = (User)Session["login"];
        if (null == user)
        {
            GlobalMethod.RedirectLogin(Response);
            return;
        }

        //如果是普通用户，只能查自己的信息
        if (user.role == UserRole.NORMAL)
        {
            this.staffDropDownList.Items.Add(user.name);
            return;
        }
       

        //要确保文库列表不能为空
        if (this.wkDropDownList.Items.Count == 0)
        {
            return;
        }
        //重新绑定
        IList<String> list = this.userDAO.SelectUserByFunction(this.wkDropDownList.SelectedValue);

        if (null == list||list.Count==0)
        {
            return;
        }
        foreach (String temp in list)
        {
            this.staffDropDownList.Items.Add(temp);
        }
    }
    /// <summary>
    /// 绑定输出数据
    /// </summary>
    private void BindData()
    {
        LogQueryCondition condition = this.GetLogQueryCondition();
        IList<OperationLogItem> result = this.logDAO.queryLogItem(condition);
        this.dailyRecordListGridView.Columns.Clear();

        if (null == result || result.Count == 0)
        {
            this.errorLabel.Text = "查询的结果为0条记录！！！";
            this.SetVisible(false);
            return;
        }
        this.SetVisible(true);
        this.dailyRecordListGridView.AutoGenerateColumns = false;
        //要显示的域
        BoundField boundField = null;
        
        
        boundField=new BoundField();
        boundField.DataField = "opType";
        boundField.HeaderText = "操作类型";
        this.dailyRecordListGridView.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "opTable";
        boundField.HeaderText = "数据库表";
        this.dailyRecordListGridView.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "opBookIn";
        boundField.HeaderText = "文库";
        this.dailyRecordListGridView.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "opPerson";
        boundField.HeaderText = "操作员";
        this.dailyRecordListGridView.Columns.Add(boundField);

        boundField = new BoundField();
        boundField.DataField = "opDate";
        boundField.HeaderText = "操作时间";
        this.dailyRecordListGridView.Columns.Add(boundField);

        HyperLinkField hyperlinkField = new HyperLinkField();
        hyperlinkField.HeaderText = "详情";
        hyperlinkField.Text = "详情";
        hyperlinkField.Target = "_blank";
        hyperlinkField.DataNavigateUrlFields = new String[] { "id" };
        hyperlinkField.DataNavigateUrlFormatString = GlobalDailyRecord.DEATIL_PAGE + "?id={0}";
        this.dailyRecordListGridView.Columns.Add(hyperlinkField);

        this.dailyRecordListGridView.DataSource = result;
        this.dailyRecordListGridView.DataBind();

        this.pageInfoLabel.Text = this.pageNumberHiddenField.Value + "/" + this.pageCountHiddenField.Value + "     共" + this.totalRecordsHiddenField.Value + "条记录";
    }
    /// <summary>
    /// 执行查询
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void searchButton_Click(object sender, EventArgs e)
    {
        if (!this.ValidateTime())
        {
            return;
        }
        if (!this.ValidateWK())
        {
            return;
        }
        if (!this.ValidateStaff())
        {
            return;
        }

        this.startHiddenField.Value = this.startTextBox.Text;
        this.endHiddenField.Value = this.endTextBox.Text;
        this.wkHiddenField.Value=this.wkDropDownList.SelectedItem.Text;
        this.staffHiddenField.Value = this.staffDropDownList.SelectedValue;
        this.opTypeHiddenField.Value = this.operationDropDownList.SelectedValue;
        this.pageNumberHiddenField.Value = "1";
        this.BindData();
    }
    //根据选择的文库动态绑定的人员
    protected void wkDropDownList_SelectedIndexChanged(object sender, EventArgs e)
    {
        this.BindStaffData();
    }

    /// <summary>
    /// 验证时间区间
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateTime()
    {
        this.errorLabel.Text = "";
        //清空时间的末位空格
        this.startTextBox.Text = this.startTextBox.Text.Trim();
        this.endTextBox.Text = this.endTextBox.Text.Trim();
        if ("".Equals(this.startTextBox.Text))
        {
            this.errorLabel.Text = "错误：时间区间的起始时间不能为空";
            return false;
        }
        if ("".Equals(this.endTextBox.Text))
        {
            this.errorLabel.Text = "错误：时间区间的结束时间不能为空";
            return false;
        }
        DateTime start;
        DateTime end;

        try
        {
            start = DateTime.Parse(this.startTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：您输入的起始时间有误";
            return false;
        }

        try
        {
            end = DateTime.Parse(this.endTextBox.Text);
        }
        catch (Exception)
        {
            this.errorLabel.Text = "错误：您输入的结束时间有误";
            return false;
        }

        if (start > end)
        {
            this.errorLabel.Text = "错误：您输入的起始时间大于结束时间";
            return false;
        }

        if (start.Year < 1900)
        {
            this.errorLabel.Text = "错误：起始时间不能小于1900年";
            return false;
        }
        if (end.Year < 1900)
        {
            this.errorLabel.Text = "错误：结束时间不能小于1900年";
            return false;
        }
        return true;
    }


    /// <summary>
    /// 验证文库
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateWK()
    {
        //清空错误信息
        this.errorLabel.Text = "";
        if (null == this.wkDropDownList.Items || this.wkDropDownList.Items.Count == 0)
        {
            this.errorLabel.Text = "错误：没有选择文库";
            return false;
        }
        if (null == this.wkDropDownList.SelectedValue || "".Equals(this.wkDropDownList.SelectedValue))
        {
            this.errorLabel.Text = "错误：没有选择文库";
            return false;
        }
        return true;
    }

    /// <summary>
    /// 验证人员
    /// </summary>
    /// <returns></returns>
    private Boolean ValidateStaff()
    {
        //清空错误信息
        this.errorLabel.Text = "";
        if (null == this.staffDropDownList.Items || this.staffDropDownList.Items.Count == 0)
        {
            this.errorLabel.Text = "错误：没有选择要查询的人员";
            return false;
        }
        if (null == this.staffDropDownList.SelectedValue || "".Equals(this.staffDropDownList.SelectedValue))
        {
            this.errorLabel.Text = "错误：没有选择要查询的人员";
            return false;
        }
        return true;
    }

    /// <summary>
    /// 获取查询条件
    /// </summary>
    /// <returns></returns>
    private LogQueryCondition GetLogQueryCondition()
    {
        LogQueryCondition condition = new LogQueryCondition();
        condition.startDate = DateTime.Parse(this.startHiddenField.Value);
        condition.endDate = DateTime.Parse(this.endHiddenField.Value);
        condition.wk = this.wkHiddenField.Value;
        condition.userName = this.staffHiddenField.Value;
        condition.opType = this.GetOPType();

        //获取符合查询条件的总的记录数
        int pageCount = this.logDAO.CountOperationLogItem(condition);

        //总的记录数
        this.totalRecordsHiddenField.Value = pageCount.ToString();
        //每页显示的记录数
        int pageSize = 20;
        //总的页数
        int totalPage = (pageCount -1) / pageSize+1;
        this.pageCountHiddenField.Value = totalPage.ToString();
        //页码
        int pageNumber = 1;
        try
        {
            pageNumber = Int32.Parse(this.pageNumberHiddenField.Value);
        }
        catch (Exception)
        {
        }
        if (pageNumber < 1)
        {
            pageNumber = 1;
        }
        if (pageNumber > totalPage)
        {
            pageNumber = totalPage;
        }
        this.pageNumberHiddenField.Value = pageNumber.ToString();
        condition.start = (pageNumber-1)*pageSize+1;
        condition.end = pageNumber*pageSize;
        return condition;
    }

    /// <summary>
    /// 获取操作类型
    /// </summary>
    /// <returns></returns>
    private String GetOPType()
    {
        String type = null;
        type = this.opTypeHiddenField.Value;
        if ("ALL".Equals(type))
        {
            type = null;
        }
        return type;
    }
    protected void submitButton_Click(object sender, EventArgs e)
    {
        if (this.ValidatePageNumber())
        {
            this.BindData();
        }
    }
    protected void headButton_Click(object sender, EventArgs e)
    {
        this.errorLabel.Text = "";
        this.pageNumberHiddenField.Value = "1";
        this.BindData();
    }
    protected void tailButton_Click(object sender, EventArgs e)
    {
        this.errorLabel.Text = "";
        this.pageNumberHiddenField.Value = this.pageCountHiddenField.Value;
        this.BindData();
    }
    /// <summary>
    /// 设置上一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void preButton_Click(object sender, EventArgs e)
    {
        this.errorLabel.Text = "";
        int pageNumber = 1;
        try
        {
            pageNumber = Int32.Parse(this.pageNumberHiddenField.Value);
        }
        catch (Exception)
        {
        }
        pageNumber--;
        this.pageNumberHiddenField.Value = pageNumber.ToString();
        this.BindData();
    }

    /// <summary>
    /// 设置下一页
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void nextButton_Click(object sender, EventArgs e)
    {
        this.errorLabel.Text = "";
        int pageNumber = 1;
        try
        {
            pageNumber = Int32.Parse(this.pageNumberHiddenField.Value);
        }
        catch (Exception)
        {
        }
        pageNumber++;
        this.pageNumberHiddenField.Value = pageNumber.ToString();
        this.BindData();
    }


    /// <summary>
    /// 验证页码输入框信息
    /// </summary>
    /// <returns></returns>
    private Boolean ValidatePageNumber()
    {
        Boolean flag = true;
        this.errorLabel.Text = "";
        this.pageNumberTextBox.Text = this.pageNumberTextBox.Text.Trim();
        if ("".Equals(this.pageNumberTextBox.Text))
        {
            flag = false;
        }
        else
        {
            int pageNumber = 1;
            try
            {
                pageNumber = Int32.Parse(this.pageNumberTextBox.Text);
                this.pageNumberHiddenField.Value = pageNumber.ToString();
            }
            catch (Exception)
            {
                flag = false;
            }
        }

        if (!flag)
        {
            this.errorLabel.Text = "请输入正确的页码";
        }
        return flag;
    }


    /// <summary>
    /// 设置控件的可见性
    /// </summary>
    /// <param name="b">true or false</param>
    private void SetVisible(Boolean b)
    {
        this.headButton.Visible = b;
        this.preButton.Visible = b;
        this.nextButton.Visible = b;
        this.tailButton.Visible = b;
        this.pageNumberTextBox.Visible = b;
        this.submitButton.Visible = b;
        this.dailyRecordListGridView.Visible = b;
        this.pageInfoLabel.Visible = b;
    }
}